package com.gp.pj.controller;


import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.gp.pj.entity.Menu;
import com.gp.pj.entity.MenuRole;
import com.gp.pj.entity.UserRole;
import com.gp.pj.service.MenuRoleService;
import com.gp.pj.service.MenuService;
import com.gp.pj.service.UserRoleService;
import com.gp.pj.utils.JWTUtils;
import com.gp.pj.utils.ResultCode;
import com.gp.pj.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chloe
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/")
public class MenuController {

    @Resource
    MenuService menuService;

    @Resource
    MenuRoleService menuRoleService;

    @Resource
    UserRoleService userRoleService;

    @ResponseBody
    @GetMapping(value = "/get_all_menus")
    public ResultUtils menuList() {
        List<Menu> menuList = menuService.list();
        menuList = menuList.stream().sorted(Comparator.comparing(Menu::getSort)).collect(Collectors.toList());
        return ResultUtils.success(menuList);
    }

    @ResponseBody
    @GetMapping(value = "/getAllMenusByRole")
    public ResultUtils menuListByRole(String token) {
        DecodedJWT decToken = JWTUtils.verifyToekn(token);
        String userId = decToken.getClaim("userId").asString();
        List<UserRole> userRoleList = userRoleService.lambdaQuery().eq(UserRole::getUserId, userId).list();
        List<MenuRole> menuRoleList = new ArrayList<>();
        for (UserRole userRole : userRoleList) {
            List<MenuRole> list = menuRoleService.lambdaQuery().eq(MenuRole::getRoleId, userRole.getRoleId()).list();
            menuRoleList = Stream.of(menuRoleList, list)
                    .flatMap(Collection::stream)
                    .distinct()
                    .collect(Collectors.toList());
        }
        if (menuRoleList.size()>0){
            List<Menu> menuList = menuService.lambdaQuery().in(Menu::getId,menuRoleList.stream().map(MenuRole::getMenuId).collect(Collectors.toList())).list();
            menuList = menuList.stream().sorted(Comparator.comparing(Menu::getSort)).collect(Collectors.toList());
            return ResultUtils.success(menuList);
        }else{
            return ResultUtils.success();
        }
    }

    @ResponseBody
    @PostMapping(value = "/saveMenu")
    public ResultUtils saveMenu(@RequestBody JSONObject jsonRe) {
        Menu menu = jsonRe.toJavaObject(Menu.class);
        boolean i = menuService.save(menu);
        if (i) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("保存菜单失败");
        }
    }


    @ResponseBody
    @PostMapping(value = "/updateMenu")
    public ResultUtils updateMenu(@RequestBody JSONObject jsonRe) {
        Menu menu = jsonRe.toJavaObject(Menu.class);
        boolean i = menuService.updateById(menu);
        if (i) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("更新菜单失败");
        }
    }

    @ResponseBody
    @PostMapping(value = "/deleteMenu")
    public ResultUtils deleteMenu(@RequestBody JSONObject jsonRe) {
        Menu menu = jsonRe.toJavaObject(Menu.class);
        boolean i = menuService.removeById(menu);
        if (i) {
            return ResultUtils.success();
        } else {
            return ResultUtils.failure("删除菜单失败");
        }
    }

}
